<template>
  <div class="px-4 h-full overflow-hidden">
    <temnplate v-if="!isShowTemperature">
      <div class="flex justify-between items-center py-2">
        <a-input-search v-model:value="searchKey" class="w-64" placeholder="姓名/助记码/住院号/床位号" @keyup.enter="getData" @search="getData" />
        <!-- <a-button type="primary" @click="open = true">导出</a-button> -->
        <div class="flex items-center">
          <div>隐藏空床</div>
          <a-switch v-model:checked="store.cardSwitch" class="ml-1 mr-3" @change="getData" />
          <BaseExport type="住院" name="待出区" code="000099" :search-params="{ status: store.tabsKey, bqbh: store.areaCode, pyjm: searchKey }"
            >导出</BaseExport
          >
        </div>
      </div>
      <BaseTable v-if="store.waitOutAreaShowType === 1" column-code="000099" :scroll="{ x: 2200 }" :loading="loading" :data-source="tableData" fill>
        <template #bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'updateUserName'">
            {{ record.inpTransferZone?.createUserName }}
          </template>
          <template v-if="column.dataIndex === 'updateTime'">
            {{ record.inpTransferZone?.createTime }}
          </template>
          <template v-if="column.dataIndex === 'brName'">
            <InPatientPopover
              v-model:open="record.openPopover"
              is-modify
              :patient-info="record"
              @change="getData"
              @click="record.openPopover = !record.openPopover"
            >
              <div class="cursor-pointer text-primary hover:underline">{{ record.brName }}</div>
            </InPatientPopover>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onPrintAdv(record, '1')">长嘱单</a-button>
            <a-button type="link" size="small" @click="onPrintAdv(record, '0')">临嘱单</a-button>
            <a-button type="link" size="small" @click="onPrintTemp(record)">体温脉搏单</a-button>
            <TableMoreButton>
              <a-button type="link" size="small" @click="onPrintNursingRecord(record, '1')">护理记录单</a-button>
              <a-button type="link" size="small" @click="onPrintBlood(record)">血糖单</a-button>
            </TableMoreButton>
          </template>
        </template>
      </BaseTable>
      <div v-else class="flex flex-1 overflow-hidden">
        <div v-if="tableData.length" class="h-full w-full flex flex-wrap overflow-y-auto content-start">
          <template v-for="item in tableData" :key="item.lastBedCode">
            <BedCard v-if="item.brName" class="m-2" is-modify :data="item" show-footer :active="currentBed?.inplsh === item.inplsh" />
            <BedCardEmpty v-else class="m-2" :bed-code="item.bedCode" :room-name="item.roomName" />
          </template>
        </div>
        <div v-else class="w-full h-full flex-center">
          <a-empty />
        </div>
        <a-spin v-if="loading" :spinning="loading" class="w-full h-full flex-center absolute" />
      </div>
    </temnplate>
    <template v-else>
      <div class="h-full">
        <TabTemperature :key="rowData.inplsh" :inplsh="rowData.inplsh" is-show-back @back="isShowTemperature = false" />
      </div>
    </template>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import TabTemperature from '@/views/in-doctor/workstation/components/TabTemperature'
import useStore from '../../store'

const searchKey = ref('')
const store = useStore()
const currentBed = ref({})
const isShowTemperature = ref(false)
const rowData = ref({})

// store.waitOutAreaShowType === 1 ? '/inp/zydj/getInpRecordList' : '/inp/zyhs/getCwAndInpRecordList'
const { loading, tableData, getData } = useTable({
  url: '/inp/zydj/getInpRecordList',
  immediate: true,
  pagination: false,
  formatParams: () => {
    return {
      status: store.tabsKey,
      bqbh: store.areaCode,
      pyjm: searchKey.value,
      hiddenEmptyBed: store.cardSwitch
    }
  }
  // formatResult: (res) => {
  //   if (!res.inplsh) {
  //     res.brName = '空床'
  //   }
  //   return res
  // }
})

const onPrintTemp = (record) => {
  rowData.value = record
  isShowTemperature.value = true
}
const onPrintBlood = async (record) => {
  const { code, msg, data } = await post('/inp/Print/printBloodGlucoseTestSlips', { inplsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onPrintNursingRecord = async (record) => {
  const { code, msg, data } = await post('/inp/InpNursingRecord/printInpNursingRecord', { inpLsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onPrintAdv = async (record, type) => {
  const { code, msg, data } = await post('/inp/Print/printTemporaryDoctorOrders', {
    sfcybd: '1',
    inpAdvice: type,
    inplsh: record.inplsh
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
</script>
